// 业务处理模块
const conn = require('db.js')
module.exports = {
    testAPI: (req, res) => {
        res.send('请求后台API接口成功')
    },
    getAllHero: (req, res) => {
        const sql = 'select * from heros'
        conn.query(sql, (err, result) => {
            if (err) return res.send({ status: 500, msg: err.message, data: null })
            res.send({ status: 200, msg: 'ok', data: result })
        })
    },
    // 添加英雄
    addHero: (req, res) => {
        // console.log(req.body);
        const hero = req.body
            // 得到当前时间对象
        let time = moment().format("YYYY-MM-DD 	HH:mm:ss");
        hero.ctime = time
        const sql = 'insert into heros set ?'
        conn.query(sql, hero, (err, result) => {
            if (err) return res.send({ status: 500, msg: err.message, data: null })
            res.send({ status: 200, msg: 'ok', data: result })
        })
    },
    getHeroById: (req, res) => {
        const id = req.params.id
        const sql = 'select * from heros where id=?'
        conn.query(sql, id, (err, result) => {
            if (err) return res.send({ status: 500, msg: err.message, data: null })
            res.send({ status: 200, msg: 'ok', data: result })
        })
    },
    updateHeroById: (req, res) => {
        const id = req.params.id
        const newInfo = req.body
        const sql = 'update heros set ? where id=?'
        conn.query(sql, [newInfo, id], (err, result) => {
            if (err) return res.send({ status: 500, msg: err.message, data: null })
            res.send({ status: 200, msg: 'ok', data: null })
        })
    },
    deleteHeroById: (req, res) => {
        const id = req.params.id
        const sql = 'update heros set isdel=1 where id=?'
        conn.query(sql, id, (err, result) => {
            if (err) return res.send({ status: 500, msg: err.message, data: null })
            res.send({ status: 200, msg: 'ok', data: null })
        })
    }
}